package org.ichow.scenegame.view
{
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	import org.ichow.debug.Log;
	import org.ichow.scenegame.controller.ToolsBarCommand;
	import org.ichow.scenegame.core.ToolsBar;
	import org.ichow.scenegame.events.NotiEvent;
	import org.ichow.scenegame.events.SceneEvent;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	/**
	 * ...
	 * 工具条代理
	 *
	 * @author M.ichow
	 */
	public class ToolsBarMediator extends Mediator
	{
		public static const NAME:String = "ToolsBarMediator";
		public static const TOOLS_BAR:String = "toolsBar";
		public static const UPDATE_TOOLS_BAR:String = "updateToolsBar";
		static public const TOOLS_BAR_CONTROL:String = "toolsBarControl";
		
		public function ToolsBarMediator( app:ToolsBar )
		{
			super( NAME , app );
		}
		
		override public function onRegister():void
		{
			super.onRegister();
			
			facade.registerCommand( TOOLS_BAR_CONTROL , ToolsBarCommand );
			//
			toolsBar.addEventListener( ToolsBar.CLOSE , onClose );
			toolsBar.addEventListener( ToolsBar.HELP , onHelp );
			toolsBar.addEventListener( ToolsBar.SOUND , onSound );
		}
		
		private function onClose( e:Event ):void
		{
			Log.trace( "--关闭游戏" );
			sendNotification( TOOLS_BAR_CONTROL , "" , ToolsBar.CLOSE );
		}
		
		private function onHelp( e:Event ):void
		{
			sendNotification( TOOLS_BAR_CONTROL , "" , ToolsBar.HELP );
		}
		
		private function onSound( e:Event ):void
		{
			sendNotification( TOOLS_BAR_CONTROL , toolsBar.isSound , ToolsBar.SOUND );
		}
		
		override public function listNotificationInterests():Array
		{
			return [ SceneEvent.ADD_TO_SCENE , TOOLS_BAR , UPDATE_TOOLS_BAR , NotiEvent.RESTART ];
		}
		
		override public function handleNotification( notification:INotification ):void
		{
			super.handleNotification( notification );
			switch ( notification.getName() )
			{
				case SceneEvent.ADD_TO_SCENE: 
					var scene:Main = notification.getBody() as Main;
					toolsBar.visible = false;
					toolsBar.isOver = false;
					toolsBar.width = Number( scene.getValueForKey( "width" ) );
					if ( !scene.contains( toolsBar ) )
						scene.addChild( toolsBar );
					break;
				case TOOLS_BAR: 
					if ( notification.getType() == "show" )
						toolsBar.visible = true;
					else
						toolsBar.visible = false;
					Log.trace( "++显示工具条: " + toolsBar.visible );
					break;
				case UPDATE_TOOLS_BAR: 
					var obj:Object = notification.getBody();
					if ( obj.score )
						toolsBar.addScore( obj.score );
					Log.trace( "@@更新工具条: " + "加分 > " + obj.score + " 次数 > " + toolsBar.addCount() );
					Log.trace( "^^播放加分动画: " + obj.score );
					sendNotification( MovieMediator.SCORE_EFFECT , { type: "+" , score: obj.score } );
					break;
				case NotiEvent.RESTART: 
					toolsBar.reset();
					toolsBar.isOver = false;
					break;
			}
		}
		
		public function getToolsInfo():Object
		{
			toolsBar.isOver = true;
			return { score: toolsBar.score , maxScore: toolsBar.maxScore , count: toolsBar.count };
		}
		
		/**
		 * Tools Bar
		 */
		protected function get toolsBar():ToolsBar
		{
			return viewComponent as ToolsBar;
		}
	}

}